﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain.Values
{
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Web.Mvc;
	using Falcon.Core.Domain;

	public class ForumPostMerge
	{
		// private fields
		private readonly IEnumerable<User> _users;

		/// <summary>
		/// Initializes a new instance of the <see cref="ForumPostMove"/> class.
		/// </summary>
		public ForumPostMerge()
		{

		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ForumPostMerge"/> class.
		/// </summary>
		/// <param name="posts">The posts.</param>
		public ForumPostMerge(IList<ForumPost> posts) : this()
		{
			this.Posts = posts;

			if (this.Posts == null)
			{
				return;
			}

			var count = this.Posts.Count;
			var builder = new StringBuilder();

			for (var i = 0; i < count; i++)
			{
				builder.Append(this.Posts[i].Body);

				if (count == (i + 1))
				{
					continue;
				}

				builder.AppendLine();
				builder.AppendLine();
			}

			this.Title = this.Posts[0].Title;
			this.Body = builder.ToString();

			_users = this.Posts.Select(x => x.User).ToList().GroupBy(x => x.Name).Select(x => x.FirstOrDefault());
		}

		/// <summary>
		/// Gets or sets the forum id.
		/// </summary>
		/// <value>The forum id.</value>
		public int ForumId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the user id.
		/// </summary>
		/// <value>The user id.</value>
		public int UserId
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the reason.
		/// </summary>
		/// <value>The reason.</value>
		public string Reason
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public string Body
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the redirect.
		/// </summary>
		/// <value>The redirect.</value>
		public string Redirect
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the posts.
		/// </summary>
		/// <value>The posts.</value>
		public IList<ForumPost> Posts
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the users.
		/// </summary>
		/// <value>The users.</value>
		public SelectList Users
		{
			get
			{
				return new SelectList(_users, "Id", "Name", this.UserId);
			}
		}

		/// <summary>
		/// Gets or sets the posts to merge.
		/// </summary>
		/// <value>The posts to merge.</value>
		public ForumPostUpdateValues[] PostsToMerge
		{
			get;
			set;
		}
	}
}